<?php

header('Content-Type: text/html;charset=UTF-8');
$_SESSION['page'] = 'thongtin';
$cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
$nt = isset($_GET['nt']) ? intval($_GET['nt']) : 0;

$typearr ='';
$typelist = '';
$typerows = Db::getRows('*', 'type', 'menu=3');
foreach ($typerows as $typerow) {
    $typearr .= $typerow['id'] .',';
    $selected = $typerow['id'] == $cid ? 'selected="selected"' : '';
    $typelist .= '<option '.$selected.' value="'.$typerow['id'].'">'.$typerow['name'].'</option>';
}
$typearr = substr($typearr, 0, strlen($typearr)-1);

$nationlist = '';
$nationrows = Db::getRows('*', 'nation');
foreach ($nationrows as $nationrow) {
    $selected = $nationrow['id'] == $nt ? 'selected="selected"' : '';
    $nationlist .= '<option '.$selected.' value="'.$nationrow['id'].'">'.$nationrow['name'].'</option>';
}

$perPage = Config::get('num_per_page');
$pageNum = isset($_GET['p']) ? intval($_GET['p']) : 1;
if ($pageNum < 1) {
    $pageNum = 1;
}
if($cid > 0)
{
    if($nt > 0)
    {
        $totalRows = Db::getOneField('COUNT(*)', 'article', 'type='.$cid.' and nation ='.$nt);
    }
    else
    {
        $totalRows = Db::getOneField('COUNT(*)', 'article', 'type='.$cid);
    }
}
else
{
    if($nt > 0)
    {
        $totalRows = Db::getOneField('COUNT(*)', 'article', 'type in ('.$typearr.') and nation ='.$nt);
    }
    else
    {
        $totalRows = Db::getOneField('COUNT(*)', 'article', 'type in ('.$typearr.')');
    }
}

$camnangpaging = Tpl::page($totalRows, $perPage, 'camnangList?cid='.$cid.'&p=');
$where = $cid > 0 ? 'type ='.$cid : 'type in ('.$typearr.')';
$where = $nt > 0 ? $where.' and nation ='.$nt : $where;
$rows = Db::getRows('*', 'article',$where .' order by id desc limit ' . ($pageNum - 1) * $perPage . ',' . $perPage);
$camnangresult = '';
foreach ($rows as $value => $row) {
    $id = $row['id'];
    $avatar = $row['img'];
    $title = $row['title'];
    $nation =Db::getOneField('name', 'nation', 'id='.$row['nation']);
    $type = Db::getOneField('name', 'type', 'id='.$row['type']);
    $posteddate = $row['posteddate'];
    $camnangresult.= '<tr>
                  <td class="check">'.++$value.'</td>
                  <td><img width="69px" height="37px" alt="product image" src="../upload/'.$avatar.'"></td>
                  <td class="item_name"><a title="" href="www.hocquocte.com/entry?id='.$id.'">'.$title.'</a></td>
                  <td class="quickaction2">'.$type.'</td>
                  <td class="quickaction2">'.$nation.'</td>
                  <td class="status">'.$posteddate.'</td>
                  <td class="tool">
                      <a onclick="return confirm(\'Bạn có chắc chắn muốn xóa?\')" href="camnangDel?id='.$id.'&cid='.$cid.'&p='.$pageNum.'" class="btPink">
                          <span>
                              <span>Xóa</span>
                          </span>
                      </a>
                      <a href="camnangEdit?id='.$id.'" class="btBlue">
                          <span>
                              <span>Sửa</span>
                          </span>
                      </a>
                  </td>
                </tr>';
}
$camnangresult = $camnangresult == '' ? '<tr><td colspan="6">chưa có dữ liệu</td></tr>' : $camnangresult;
?>

